package cn.zs.supply.common.core.domain.result;

import cn.zs.supply.common.constants.enums.PubErrCode;
import cn.zs.supply.common.core.domain.vo.BaseVo;
import cn.zs.supply.common.core.exception.BusiException;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.ToString;

@Data
@ToString(callSuper = true)
public class ObjectResult<T extends BaseVo> extends BaseResult {

    @JsonProperty("data")
    private T result;

    public ObjectResult() {

    }

    public ObjectResult(Boolean isSuccess, String errcode, String errmsg) {
        super(isSuccess, errcode, errmsg);
    }

    public T getResult(boolean isThrowException) {
        if (isThrowException) {
            if (getSuccess() != null && !getSuccess()) {
                throw new BusiException(getErrcode(), getErrmsg());
            } else if (result == null) {
                throw new BusiException(PubErrCode.EMPTY_DATA);
            }
        }
        return result;
    }
}

